package ar.uba.dc.tl.mylanga.symbol;

import java.util.Collection;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Map;

public class SymbolFactory {

	private Map<String, Symbol> symbols;

	public SymbolFactory() {
		this.symbols = new IdentityHashMap<>();
	}

	public Symbol get(String name) {
		assert name != null;

		String trimmedName = name.trim();

		assert !trimmedName.isEmpty();

		String symbolName = trimmedName.intern();

		this.ensureExists(symbolName);

		return this.symbols.get(symbolName);
	}

	public Collection<Symbol> getSymbols() {
		return Collections.unmodifiableCollection(this.symbols.values());
	}

	private void ensureExists(String name) {
		if (!this.symbols.containsKey(name)) {
			this.symbols.put(name, new Symbol(name));
		}
	}
}
